<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.UserMethods"%>
<!doctype html>
<html><head>
    <meta charset="utf-8">

				<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
    <link href="main/assets/css/main.css" rel="stylesheet">
    
	<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

    <style type="text/css">
      body {
        padding-top: 60px;
        width:100%
      }
    </style>

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="main/assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="main/assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="main/assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="main/assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="main/assets/ico/apple-touch-icon-57-precomposed.png">
<script>
 

$("a.users").click(function() {
		$("#mainPage_content").hide().fadeIn(1000).load("friends/userProfile.jsp?user_id="+ $(this).attr('id'));
		return false;
	});

</script>


  </head>
  <body>
  <br>
  <%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						UserMethods um = new UserMethods();
						MessageMethods msg_m = new MessageMethods();
						 Integer user_id = (Integer) session.getAttribute("user_id");
							response.setContentType("text/html");
							if (user_id != null) {
						User u = um.id_to_user(sessionFactory, s, user_id);
						List<Message> msglist = msg_m.list_by_receiver(sessionFactory, s, user_id);
						
					%> 
  	
    <div class="container">

	  <!-- FIRST ROW OF BLOCKS -->     
      <div class="row">

      <!-- USER PROFILE BLOCK -->
        <div class="col-sm-3 col-lg-3" style="width:50%;">
      		<div class="dash-unit">
	      		<dtitle><%out.println("<a class=\"users\" id=\""+ (Integer) session.getAttribute("user_id") + "\" href=\"#\">" + "Twój profil" + "</a>"); %></dtitle>
	      		<hr>
				<div class="thumbnail">
				<img src="main/userAvatar.jsp?user_id=<%=(Integer) session.getAttribute("user_id") %>" alt="UserAvatar" class="img-circle" style="height: 115px">
				
				</div><!-- /thumbnail -->
				<h1><%=u.getUsername() %></h1>
				<h3><%=u.getEmail() %></h3>
				<br>
					<div class="info-user">
						<span aria-hidden="true" class="li_user fs1"></span>
						<span aria-hidden="true" class="li_settings fs1"></span>
						<span aria-hidden="true" class="li_mail fs1"></span>
						<span aria-hidden="true" class="li_key fs1"></span>
					</div>
				</div>
        </div>

      <!-- LAST USER BLOCK -->     
				<div class="col-sm-3 col-lg-3" style="width:50%; text-align: center;">
      		<div class="dash-unit" >
	      		<dtitle>Powitaj nowych graczy!</dtitle>
	      		<hr>
	      			<%List<User> latest =  um.listLatestRegisteredUsers(sessionFactory, s); 
	      			for (User last: latest){
	      			if(!last.equals(um.id_to_user(sessionFactory, s, user_id))){%>
	      				<img src="main/userAvatar.jsp?user_id=<%=last.getUser_id()%>" alt="UserAvatar" style="height: 50px">
	      				<br>
	      				<p><%=last.getUsername() %></p>
	      				<!-- <p><bold>Liverpool, England</bold></p> -->
	      				<%}
	      			else
	      			{
	      				if(latest.size()==1){
	      					out.println("Nie ma zadnych nowych graczy oprocz Ciebie");
	      				}
	      			}
	      				}%>
				</div>
        
        
      </div><!-- /row -->
      
      
      
      
	  <!-- SECOND ROW OF BLOCKS -->     
      <div class="row">
        <div class="col-sm-3 col-lg-3" style="width:100%;">
       <!-- MAIL BLOCK -->
      		<div class="dash-unit">
      		<dtitle><a href="http://localhost:8080/Thesis/social/index.jsp?page=messages&group=inbox">Skrzynka odbiorcza</a></dtitle>
      		<hr>
      			         <%
		if (msglist.size() > 0) {
			%>
			<div class="framemail">
    			<div class="window">
			        <ul class="mail">
			        <%
	for (Message m1 : msglist) {
		int sender_id = m1.getSender_id();
		UserMethods u_m = new UserMethods();
		User sender = u_m.id_to_user(sessionFactory, s, sender_id);
		String sender_nick = sender.getUsername();
		String subject = m1.getSubject();
		String date = m1.getSend_date();
		String time = m1.getSend_time();
		
		if (m1.getReceive_date() == null){
		%>
		
		<li>
			                <i class="unread"></i>
			                <img class="avatar" src="main/userAvatar.jsp?user_id=<%=sender_id %>" alt="avatar">
			                <p class="sender"><% out.println(sender_nick); %></p>
			                <p class="message"><strong><% out.println("<a class=\"subjects\" id=\""+ m1.getMessage_id() + "\" href=\"#\">" + subject + "</a>"); %></strong> - tresc...</p>
			               
			            </li>
			            
			    <% }
		else {
		%>
   
			            <li>
			                <i class="read"></i>
			                <img class="avatar" src="main/userAvatar.jsp?user_id=<%=sender_id %>" alt="avatar">
			                <p class="sender"><% out.println(sender_nick); %></p>
			                <p class="message"><strong><% out.println("<a class=\"subjects\" id=\""+ m1.getMessage_id() + "\" href=\"#\">" + subject + "</a>"); %></strong> - tresc...</p>
			                
			            </li>
			            <%} %>

   
		<%
		}
		}
		else 
		out.println("Nie masz jeszcze zadnych wiadomosci w swojej skrzynce odbiorczej");%>
		 </ul>
		</div><!-- /dash-unit -->
    </div><!-- /span3 -->
    </div>
    </div>

	</div>
     
    
      <div class="row">
      	<div class="col-sm-3 col-lg-3" style="width:100%">
      	   
      		<div class="half-unit" >
	      		<dtitle><a href="http://localhost:8080/Thesis/social/index.jsp?page=games&group=rated">Poznaj je wszystkie!</a></dtitle>
	      		<hr>
	      		<div class="cont">
	      		<%RateMethods rm = new RateMethods();
	      		GameMethods gm = new GameMethods();
	      		
	      		int ile = rm.findGameRateByUser(sessionFactory, s, user_id).size();
	      		int all = gm.listAllGames(sessionFactory, s).size();
	      		%>
					<p><bold><%=ile %></bold> | Ocenionych gier</p>
					<% float percentage = (float) ((ile *100)/ all); 
					%>
				</div>
		             <div class="progress">
				        <div class="progress-bar" role="progressbar" aria-valuenow="<%=percentage %>" aria-valuemin="0" aria-valuemax="100" style="width:<%=percentage %>%;"><span class="sr-only"><%=percentage %>% Complete</span>
					        
				        </div>
				     </div>
      		</div>
      	</div>

   
      	
				<% NewsMethods nm = new NewsMethods();
				if (nm.listAllNews(sessionFactory, s).size()>0){ %>
				<div class="col-sm-3 col-lg-3" style="width:100%">
      		<div class="dash-unit">
	      		<dtitle>Newsy</dtitle>
	      		<hr>
				<div class="info-user">
					<span aria-hidden="true" class="li_news fs2"></span>
				</div>
				<br>
				<div class="text">
				<%
				for (News n: nm.listAllNews(sessionFactory, s)){%>
				
				
      				<p><b><%=n.getSubject() %></b> <%=n.getContent() %></p>
      				<p><grey>
      				<%//out.println("Zamieszczone: <script>	jQuery.timeago("+ n.getAdd_date()+")");  %>
      				</grey></p>
      			
				
				<%} %>
				</div>
				<%
			}else {%>
				<div class="col-sm-3 col-lg-3" style="width:100%">
      		<div class="dash-unit">
	      		<dtitle>Newsy</dtitle>
	      		<hr>
				<div class="info-user">
					<span aria-hidden="true" class="li_news fs2"></span>
				</div>
				<br>
				<div class="text">
      				<p><b>Nie ma zadnych wiadomosci</b></p>
      			</div>
      			
				<%}%>
      			
      		</div>
      	</div>
      </div>
      
  
		
			</div>
			
		

		</div>
     
      
      

<%}
							else
							out.println("Aby zobaczyć dashboard musisz być zalogowany");%>

    <script type="text/javascript" src="main/assets/js/bootstrap.js"></script>
	<script type="text/javascript" src="main/assets/js/lineandbars.js"></script>
    
	<script type="text/javascript" src="main/assets/js/noty/themes/default.js"></script>


    <script type="text/javascript" src="main/assets/js/admin.js"></script>
  
</body></html>